var _begin="";
var _end="";
/**
 * Operation Log
 */
var log = {
		

	init : function() {
		$('#form01').ajaxpager({
			target : '#content'
		});
		/*$('#begintime').datebox({
			onChange : function(newValue, oldValue) {
				log.filtration();
			},

		});
		$('#endtime').datebox({
			onChange : function(newValue, oldValue) {
				log.filtration();
			},
		});*/
		$.ajax({
			type : "post",
			url : 'operationlog/userName',
			dataType : "json",
			success : function(json) {
				$('#userFiltration').combobox({
					data : json.data,
					valueField : 'id',
					textField : 'name',
					width:'100',
					onSelect : function(record) {
						log.filtration();
					}
				});
			}
		});
	},
	/**
	 * Operation Log List
	 */
	list : function() {
		var loglistUrl = baseURL + "/system/operationlog/list"
		$.ajax({
			type : "get",
			url : loglistUrl,
			dataType : "html",
			success : function(html) {
				// Request successfully and fill the data.
				$("#content").empty().html(html);
			}
		});
	},
	
	
	/**
	 * filtration 
	 */
	filtration : function(){
		var begintime = $('#begintime').datebox('getValue');	
		var endtime = $('#endtime').datebox('getValue');
		var userId = $('#userFiltration').combobox('getValue');
		var dangerLevel = $("#danger").val();
		var operateResult = $("#operateResult").val();
		var loglistUrl = baseURL + "/system/operationlog/filtration";
		$('#begintimeForPager').attr({value : begintime});
		$('#endtimeForPager').attr({value : endtime});
		$('#userIdForPager').attr({value : userId});
		$('#dangerForPager').attr({value : dangerLevel});
		$('#operateResultForPager').attr({value : operateResult});
		$.ajax({
			type : "post",
			url : loglistUrl,
			data : {beginTime : begintime, endTime : endtime,userId : userId,dangerLevel : dangerLevel,operateResult : operateResult},
			dataType : "html",
			success : function(html) {
				// Request successfully and fill the data.
				$("#content").empty().html(html);
			}
		});
	},
	
	exportLog : function() {
		var begintime = $('#begintime').datebox('getValue');	
		var endtime = $('#endtime').datebox('getValue');
		var userId = $('#userFiltration').combobox('getValue');
		var dangerLevel = $("#danger").val();
		var operateResult = $("#operateResult").val();
		var Url = baseURL + "/system/operationlog/exportLog" + "?beginTime="
				+ begintime + "&endTime=" + endtime + "&userId=" + userId + 
				"&dangerLevel=" + dangerLevel + "&operateResult=" + operateResult;
		window.open(Url);
	},
	
	/**
	 * Remove logs
	 */
	remove : function(){
		var begintime = $('#begintime').datebox('getValue');	
		var endtime = $('#endtime').datebox('getValue');
		var userId = $('#userFiltration').combobox('getValue');
		var dangerLevel = $("#danger").val();
		var operateResult = $("#operateResult").val();
		var logId ={};
		if (begintime == "" && endtime == "" && userId == "" && dangerLevel == "" && operateResult ==""){
			var confirmMessage = localeMsg.log.clear;
			common.confirmDialog(confirmMessage,log.removeDel);
		}
		else{
			var confirmMessage = localeMsg.log.deleteConfirm;
				common.confirmDialog(confirmMessage,log.removeDel);
		}
	},
	
	removeDel : function() {
		//  Remove  records
		var begintime = $('#begintime').datebox('getValue');	
		var endtime = $('#endtime').datebox('getValue');
		var userId = $('#userFiltration').combobox('getValue');
		var dangerLevel = $("#danger").val();
		var operateResult = $("#operateResult").val();
		$.ajax({
			type : "post",
			url : baseURL + "/system/operationlog/remove",
			data : {beginTime : begintime, endTime : endtime,userId : userId,dangerLevel : dangerLevel,operateResult : operateResult},
			traditional: true,
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						success : true,
						title : localeMsg.common.info,
						text : localeMsg.common.deleteSuccess,
						callback : function() {
							log.list();
							 $('#userFiltration').combobox('clear');
							 $('#begintime').datebox('clear');
							 $('#endtime').datebox('clear');
							 $("#danger").val('');
							 $("#operateResult").val('');
						}
					});
				}else {
					common.showTip({
						success : false,
						title : localeMsg.common.warn,
						text : localeMsg.common.deleteFailed,
					});
				}
			}
		});
	},
	
	quick_search:function(){ //bieyd
		
		var time_flag=$("#quick_search").find("option:selected").val();
		var userId = $('#userFiltration').combobox('getValue');
		var dangerLevel = $("#danger").val();
		var operateResult = $("#operateResult").val();
		var loglistUrl = baseURL + "/system/operationlog/filtration";
		var mydate=new Date();
		var endtime="";
		var begintime="";
		switch(time_flag){
			case ("0")://默认
				endtime=mydate.toLocaleDateString();
				break;
			case("1")://今天
				endtime=mydate.Format("yyyy-MM-dd");
				begintime=endtime;
				break;
			case ("2")://昨天
				var dd=new Date();
				dd.setDate(dd.getDate()-1);
				endtime=dd.Format("yyyy-MM-dd");
				begintime=endtime;
				break;
			case ("3")://本周
				begintime=dateRangeUtil.getCurrentWeek()[0].Format("yyyy-MM-dd");
				endtime=mydate.Format("yyyy-MM-dd");
				break;
			case ("4")://上周
				begintime=dateRangeUtil.getPreviousWeek()[0].Format("yyyy-MM-dd"); 
				endtime=dateRangeUtil.getPreviousWeek()[1].Format("yyyy-MM-dd"); 
				break;
			case ("5")://本月
				begintime=dateRangeUtil.getCurrentMonth()[0].Format("yyyy-MM-dd");
				endtime=mydate.Format("yyyy-MM-dd");
				break;
			case ("6")://上月
				begintime=dateRangeUtil.getPreviousMonth()[0].Format("yyyy-MM-dd");
				endtime=dateRangeUtil.getPreviousMonth()[1].Format("yyyy-MM-dd");
				break;
		}
		
		$.ajax({
			type : "post",
			url : loglistUrl,
			data : {beginTime : begintime, endTime : endtime,userId : userId,dangerLevel : dangerLevel,operateResult : operateResult},
			dataType : "html",
			success : function(html) {
				// Request successfully and fill the data.
				$("#content").empty().html(html);
				$('#begintime').datebox('setValue',begintime);	
				$('#endtime').datebox('setValue',endtime);
			}
		});
	},
	
};

/**
* 日期范围工具类
*/
var dateRangeUtil = (function () {
    /***
    * 获得当前时间
    */
    this.getCurrentDate = function () {
        return new Date();
    };

    /***
    * 获得本周起止时间
    */
    this.getCurrentWeek = function () {
        //起止日期数组  
        var startStop = new Array();
        //获取当前时间  
        var currentDate = this.getCurrentDate();
        //返回date是一周中的某一天  
        var week = currentDate.getDay();
        //返回date是一个月中的某一天  
        var month = currentDate.getDate();

        //一天的毫秒数  
        var millisecond = 1000 * 60 * 60 * 24;
        //减去的天数  
        var minusDay = week != 0 ? week - 1 : 6;
        //alert(minusDay);  
        //本周 周一  
        var monday = new Date(currentDate.getTime() - (minusDay * millisecond));
        //本周 周日  
        var sunday = new Date(monday.getTime() + (6 * millisecond));
        //添加本周时间  
        startStop.push(monday); //本周起始时间  
        //添加本周最后一天时间  
        startStop.push(sunday); //本周终止时间  
        //返回  
        return startStop;
    };

    /***
    * 获得本月的起止时间
    */
    this.getCurrentMonth = function () {
        //起止日期数组  
        var startStop = new Array();
        //获取当前时间  
        var currentDate = this.getCurrentDate();
        //获得当前月份0-11  
        var currentMonth = currentDate.getMonth();
        //获得当前年份4位年  
        var currentYear = currentDate.getFullYear();
        //求出本月第一天  
        var firstDay = new Date(currentYear, currentMonth, 1);


        //当为12月的时候年份需要加1  
        //月份需要更新为0 也就是下一年的第一个月  
        if (currentMonth == 11) {
            currentYear++;
            currentMonth = 0; //就为  
        } else {
            //否则只是月份增加,以便求的下一月的第一天  
            currentMonth++;
        }


        //一天的毫秒数  
        var millisecond = 1000 * 60 * 60 * 24;
        //下月的第一天  
        var nextMonthDayOne = new Date(currentYear, currentMonth, 1);
        //求出上月的最后一天  
        var lastDay = new Date(nextMonthDayOne.getTime() - millisecond);

        //添加至数组中返回  
        startStop.push(firstDay);
        startStop.push(lastDay);
        //返回  
        return startStop;
    };



    /**
    * 获得该月的天数
    * @param year年份
    * @param month月份
    * */
    this.getMonthDays = function (year, month) {
        //本月第一天 1-31  
        var relativeDate = new Date(year, month, 1);
        //获得当前月份0-11  
        var relativeMonth = relativeDate.getMonth();
        //获得当前年份4位年  
        var relativeYear = relativeDate.getFullYear();

        //当为12月的时候年份需要加1  
        //月份需要更新为0 也就是下一年的第一个月  
        if (relativeMonth == 11) {
            relativeYear++;
            relativeMonth = 0;
        } else {
            //否则只是月份增加,以便求的下一月的第一天  
            relativeMonth++;
        }
        //一天的毫秒数  
        var millisecond = 1000 * 60 * 60 * 24;
        //下月的第一天  
        var nextMonthDayOne = new Date(relativeYear, relativeMonth, 1);
        //返回得到上月的最后一天,也就是本月总天数  
        return new Date(nextMonthDayOne.getTime() - millisecond).getDate();
    };

    /**
    * 返回上一个月的第一天Date类型
    * @param year 年
    * @param month 月
    **/
    this.getPriorMonthFirstDay = function (year, month) {
        //年份为0代表,是本年的第一月,所以不能减  
        if (month == 0) {
            month = 11; //月份为上年的最后月份  
            year--; //年份减1  
            return new Date(year, month, 1);
        }
        //否则,只减去月份  
        month--;
        return new Date(year, month, 1); ;
    };

    /**
    * 获得上一月的起止日期
    * ***/
    this.getPreviousMonth = function () {
        //起止日期数组  
        var startStop = new Array();
        //获取当前时间  
        var currentDate = this.getCurrentDate();
        //获得当前月份0-11  
        var currentMonth = currentDate.getMonth();
        //获得当前年份4位年  
        var currentYear = currentDate.getFullYear();
        //获得上一个月的第一天  
        var priorMonthFirstDay = this.getPriorMonthFirstDay(currentYear, currentMonth);
        //获得上一月的最后一天  
        var priorMonthLastDay = new Date(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth(), this.getMonthDays(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth()));
        //添加至数组  
        startStop.push(priorMonthFirstDay);
        startStop.push(priorMonthLastDay);
        //返回  
        return startStop;
    };


    /**
    * 获得上一周的起止日期
    * **/
    this.getPreviousWeek = function () {
        //起止日期数组  
        var startStop = new Array();
        //获取当前时间  
        var currentDate = this.getCurrentDate();
        //返回date是一周中的某一天  
        var week = currentDate.getDay();
        //返回date是一个月中的某一天  
        var month = currentDate.getDate();
        //一天的毫秒数  
        var millisecond = 1000 * 60 * 60 * 24;
        //减去的天数  
        var minusDay = week != 0 ? week - 1 : 6;
        //获得当前周的第一天  
        var currentWeekDayOne = new Date(currentDate.getTime() - (millisecond * minusDay));
        //上周最后一天即本周开始的前一天  
        var priorWeekLastDay = new Date(currentWeekDayOne.getTime() - millisecond);
        //上周的第一天  
        var priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (millisecond * 6));

        //添加至数组  
        startStop.push(priorWeekFirstDay);
        startStop.push(priorWeekLastDay);

        return startStop;
    };

    return this;
})();



		   